package experiment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import ui.FileUtils;

public class ExperimentLogger {

	public Experiment experiment;
	public ArrayList<ExperimentLog> logs;
	public ExperimentLog currentLog;
	
	public File actionLogFile;
	public File answerLogFile;
	public File fullLogFile;
	
	public ExperimentLogger(Experiment experiment) {
		this.experiment = experiment;
		this.logs = new ArrayList<ExperimentLog>();
		// TODO Auto-generated constructor stub
		
		File[] files = FileUtils.findFiles(Settings.logsFolderPath);
		
		int fileCount = FileUtils.count("participant_"+ experiment.getParticipant().getID()+"_fullLog_", files);
	
		this.fullLogFile = new File(Settings.logsFolderPath + "participant_"+ experiment.getParticipant().getID()+"_fullLog_" +fileCount + ".expLog");
		try 
		{
			this.fullLogFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		fileCount = FileUtils.count("participant_"+ experiment.getParticipant().getID()+"_answerLog_", files);	
		this.answerLogFile = new File(Settings.logsFolderPath + "participant_"+ experiment.getParticipant().getID()+"_answerLog_" +fileCount + ".expLog");
		try 
		{
			this.answerLogFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		fileCount = FileUtils.count("participant_"+ experiment.getParticipant().getID()+"_actionLog_", files);
		this.actionLogFile = new File(Settings.logsFolderPath + "participant_"+ experiment.getParticipant().getID()+"_actionLog_" +fileCount + ".expLog");
		try 
		{
			this.actionLogFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void endQuestion(int answerId)
	{
		currentLog.setEndTime(System.currentTimeMillis());
		currentLog.setElapsedTime(currentLog.getEndTime() - currentLog.getStartTime());
		currentLog.setAnswerId(answerId);
		
		try
		{
			FileWriter fw = new FileWriter(this.answerLogFile, true);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(this.currentLog.toString()+"\n");
			 
			output.flush();
			output.close();
		}
		catch(IOException ioe)
		{
			System.out.print("Erreur : ");
			ioe.printStackTrace();
		}
	}
	
	public void addFullLog(String log)
	{
		try
		{
			FileWriter fw = new FileWriter(this.fullLogFile, true);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(log);
			 
			output.flush();
			output.close();
		}
		catch(IOException ioe)
		{
			System.out.print("Erreur : ");
			ioe.printStackTrace();
		}
	}

	public void addExperimentAction(ExperimentAction action) {
		// TODO Auto-generated method stub
		try
		{
			FileWriter fw = new FileWriter(this.actionLogFile, true);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(action.toString()+"\n");
			 
			output.flush();
			output.close();
		}
		catch(IOException ioe)
		{
			System.out.print("Erreur : ");
			ioe.printStackTrace();
		}
		currentLog.addAction(action);
	}

	public void startQuestion(Question question) {
		// TODO Auto-generated method stub
		ExperimentLog log = new ExperimentLog(question);
		this.logs.add(log);
		this.currentLog = log;
		// TODO Auto-generated method stub
		log.setStartTime(System.currentTimeMillis());
	}
}
